namespace Billing
{
    using AjaxControlToolkit;
    using System;
    using System.Data;
    using System.Web.UI;
    using System.Web.UI.WebControls;

    public class ucAgency : UserControl
    {
        protected UpdatePanel ActionPanel;
        protected Button btnDelete;
        protected ConfirmButtonExtender btnDelete_ConfirmButtonExtender;
        protected Button btnReset;
        protected Button btnSave;
        private DataTable dt;
        protected Panel PanelDetails;
        protected RadioButtonList RListIsActive;
        protected RadioButtonList RListIsMinistry;
        protected Timer TimerCloseIt;
        protected TextBox txtAgencyID;
        protected TextBox txtAgencyName;
        protected ucHighlight ucHighlight1;
        protected UpdatePanel updatePanel3;
        protected UpdateProgress UpdateProgress1;
        protected HiddenField xDialogID;
        protected HiddenField xPrimaryKey;

        public void BindEditData()
        {
            if ((this.dt != null) && (this.dt.Rows.Count > 0))
            {
                this.xPrimaryKey.Value = this.dt.Rows[0]["ID"].ToString();
                this.txtAgencyID.Text = this.dt.Rows[0]["AgencyIDName"].ToString();
                this.ViewState["AgencyIDName"] = this.dt.Rows[0]["AgencyIDName"].ToString();
                this.txtAgencyName.Text = this.dt.Rows[0]["AgencyName"].ToString();
                this.RListIsMinistry.SelectedIndex = -1;
                this.RListIsMinistry.Items.FindByValue(this.dt.Rows[0]["IsMinistry"].ToString()).Selected = true;
                this.RListIsActive.SelectedIndex = -1;
                this.RListIsActive.Items.FindByValue(this.dt.Rows[0]["AgencyStatus"].ToString()).Selected = true;
            }
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            ClsAgency tblAgency = new ClsAgency();
            if (Convert.ToInt64(this.xPrimaryKey.Value) > 0)
            {
                tblAgency.ID = Convert.ToInt64(this.xPrimaryKey.Value);
                tblAgency.Delete();
                ClsUtilityHelper.ResetFormControlValues(this);
                this.TimerCloseIt.Enabled = true;
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (this.txtAgencyID.Text == "")
            {
                this.ucHighlight1.ErrorMessage = "Agency ID required";
            }
            else
            {
                bool flag = ClsValidationHelper.IsRecordExist("Agency", "AgencyIDName", this.txtAgencyID.Text);
                if (flag && (Convert.ToInt64(this.xPrimaryKey.Value) == 0))
                {
                    this.ucHighlight1.ErrorMessage = " Record Exists. ";
                }
                else if ((flag && (Convert.ToInt64(this.xPrimaryKey.Value) > 0)) && (this.ViewState["AgencyIDName"].ToString().ToUpper() != this.txtAgencyID.Text.ToUpper()))
                {
                    this.ucHighlight1.ErrorMessage = " Record Exists. ";
                }
                else
                {
                    ClsAgency cls = new ClsAgency {
                        AgencyIDName = this.txtAgencyID.Text,
                        AgencyName = this.txtAgencyName.Text,
                        IsMinistry = Convert.ToBoolean(this.RListIsMinistry.SelectedValue),
                        AgencyStatus = Convert.ToBoolean(this.RListIsActive.SelectedValue)
                    };
                    if (Convert.ToInt64(this.xPrimaryKey.Value) > 0)
                    {
                        cls.ID = Convert.ToInt64(this.xPrimaryKey.Value);
                        cls.ModifiedBy = (long) base.Session["UserID"];
                        cls.Update();
                        this.ucHighlight1.Message = " Record Updated ";
                    }
                    else
                    {
                        cls.CreatedBy = (long) base.Session["UserID"];
                        cls.Save();
                        this.ucHighlight1.Message = " Record Saved. ";
                    }
                    ClsUtilityHelper.ResetFormControlValues(this);
                    this.TimerCloseIt.Enabled = true;
                }
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            this.BindEditData();
            this.ucHighlight1.CloseAll();
        }

        protected void TimerCloseIt_Tick(object sender, EventArgs e)
        {
            ClsUtilityHelper.CloseJQueryDialog(this.DialogName, this);
            this.TimerCloseIt.Enabled = false;
            ClsUtilityHelper.EnableFormControlValues(this);
            this.ucHighlight1.CloseAll();
        }

        public bool Create0
        {
            get
            {
                return this.btnSave.Visible;
            }
            set
            {
                this.btnSave.Visible = value;
            }
        }

        public bool Delete0
        {
            get
            {
                return this.btnDelete.Visible;
            }
            set
            {
                this.btnDelete.Visible = value;
            }
        }

        public string DialogName
        {
            get
            {
                return this.xDialogID.Value;
            }
            set
            {
                this.xDialogID.Value = value;
            }
        }

        public DataTable EditData
        {
            get
            {
                return this.dt;
            }
            set
            {
                this.dt = value;
                this.BindEditData();
                this.TimerCloseIt.Enabled = false;
            }
        }

        public bool Update0
        {
            get
            {
                return this.btnSave.Visible;
            }
            set
            {
                this.btnSave.Visible = value;
            }
        }
    }
}
